﻿local main_spell = "afk"
maxmana = UnitPowerMax("player");

facerolldb = {}
facerollgcdcheck = {}

faceroll_profile = "default"

faceroll_filter = {}
local afk = 
		{
			id=0,
			icon=0,
			casttime=1,
			cd=0,
			prio=-999,
		};

local spellHaste = GetCombatRatingBonus(20)

faceroll_events = {}

faceroll_eventFrame = CreateFrame("Frame")
faceroll_eventFrame:SetScript("OnEvent", function(faceroll_events_ADDON_LOADED, event, ...)
  faceroll_events[event](...)
end)

faceroll_eventFrame:RegisterEvent("ADDON_LOADED")
faceroll_eventFrame:RegisterEvent("PLAYER_TARGET_CHANGED")
faceroll_eventFrame:RegisterEvent("COMBAT_RATING_UPDATE") 
faceroll_eventFrame:RegisterEvent("PLAYER_REGEN_DISABLED")
--faceroll_eventFrame:RegisterEvent("UNIT_SPELLCAST_INTERRUPTIBLE")
--faceroll_eventFrame:RegisterEvent("UNIT_SPELLCAST_NOT_INTERRUPTIBLE")

function faceroll_events.UNIT_SPELLCAST_INTERRUPTIBLE(arg1, arg2, arg3)
	if arg1 then print (arg1) end
	if arg2 then print (arg2) end
	if arg3 then print (arg3) end
	interruptable_spells_db.arg2 = true
end

function faceroll_events.UNIT_SPELLCAST_INTERRUPTIBLE(arg1, arg2, arg3)
	if arg1 then print (arg1) end
	if arg2 then print (arg2) end
	if arg3 then print (arg3) end
end

function faceroll_events.PLAYER_REGEN_DISABLED(addon)
	maxmana = UnitPowerMax("player");
end

function faceroll_events.COMBAT_RATING_UPDATE(unit)
	if unit == "player" then
		spellHaste = GetCombatRatingBonus(20) 
	end
end

function faceroll_events.PLAYER_TARGET_CHANGED(...)
	if UnitName("target") == nil or UnitIsFriend("player","target") ~= nil or UnitHealth("target") == 0 then
		faceroll_displayFrame_current:Hide()
		faceroll_displayFrame_next:Hide()
		faceroll_displayFrame_misc:Hide()
		faceroll_displayFrame_int:Hide()
	else
		faceroll_displayFrame_current:Show()
		faceroll_displayFrame_next:Show()
		faceroll_displayFrame_misc:Show()
		faceroll_displayFrame_int:Show()
		faceroll_DecideSpells()
	end
end

function faceroll_events.ADDON_LOADED(addon)
	
	local localizedClass, englishClass = UnitClass("player");
	faceroll_profile= faceroll_generate_profile();
	
	--print (faceroll_profile)
	if (not facerolldb[faceroll_profile]) then
		facerolldb[faceroll_profile] = {}
	end
	
	for i,j in pairs(facerolldb[faceroll_profile]) do
		j["lastcast"]=0
	end

	faceroll_filter.cd_filter = cd_filter;
	faceroll_filter.combo_filter = combo_filter;
	faceroll_filter.debuff_filter = debuff_filter;
	faceroll_filter.double_filter = double_filter;
	faceroll_filter.resource_filter = resource_filter; 
	faceroll_filter.selfbuff_filter = selfbuff_filter;
	faceroll_filter.notselfbuff_filter = notselfbuff_filter;
	faceroll_filter.selfdebuff_filter = selfdebuff_filter;
	faceroll_filter.notselfdebuff_filter = notselfdebuff_filter;
	faceroll_filter.targetdebuff_filter = targetdebuff_filter;
	faceroll_filter.nottargetdefbuff_filter = nottargetdefbuff_filter;
	faceroll_filter.power_filter = power_filter;
	faceroll_filter.interrupt_filter = interrupt_filter;
  
	faceroll_CreateGUI()   
end

function faceroll_DecideSpells()
	faceroll_timeSinceLastUpdate = 0;
	local guid = UnitGUID("target")
		if  UnitName("target") == nil or UnitIsFriend("player","target") ~= nil or UnitHealth("target") == 0 then
		return 
	end
	
	faceroll_profile= faceroll_generate_profile();

	if guid == nil then
		faceroll_textureList["current"]:SetTexture(nil)
		faceroll_textureList["next"]:SetTexture(nil)
		faceroll_textureList["misc"]:SetTexture(nil)
		faceroll_textureList["int"]:SetTexture(nil)
		return
	end

	local spell = ""
	local nextspell = ""

	local predict_offset = 1.5

	spell = faceroll_NextSpell(nil)
	if spell then
		faceroll_textureList["current"]:SetTexture(GetSpellTexture(spell["icon"]))
	else
		faceroll_textureList["current"]:SetTexture(nil)
	end
	nextspell=faceroll_NextSpell(spell)

	if nextspell then
		faceroll_textureList["next"]:SetTexture(GetSpellTexture(nextspell["icon"]))
	else
		faceroll_textureList["next"]:SetTexture(nil)
	end
	
	--faceroll_textureList["int"]:SetTexture("Interface\\PVPFrame\\PVP-Currency-Alliance")

end

function faceroll_NextSpell(prev_spell)

	local predict_offset = 0;
	if (prev_spell) then
		predict_offset = prev_spell["casttime"]
	end
	local s = afk;
	local p = 0;
	
	for i,j in pairs(facerolldb[faceroll_profile]) do	
		local b = true
		for y,z in pairs(faceroll_filter) do
			b = b and z(j, prev_spell)
			if (not b) then 
				if prev_spell then
				--	print("not using " .. tostring(i) .. " because of " .. tostring(y))
				end
				break
			end 
		end
		if b then
			pt = j["prio"]
			if (pt > p) then
				p = pt;
				s = j;
			end		
		end
	end
	return s;
end

SLASH_GETBUFFSWORLD1, SLASH_GETBUFFSWORLD2 = '/getallbuffs', '/gb'; 

function faceroll_getallbuffids(arg)
	local i = 0
	print ("me:")
	for i=1, 40 do
		name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellId = UnitAura("player", i) 
		if name then
			print (name .. " " .. spellId)
		end
	end
	print ("target:")
	for i=1, 40 do
		name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellId = UnitDebuff("target", i) 
		if name then
			print (name .. " " .. spellId)
		end
	end
	print ("Spell on Button 1: " .. select(2,GetActionInfo(1)))
end

SlashCmdList.GETBUFFSWORLD = faceroll_getallbuffids;

function faceroll_generate_profile()
	local i=0
	local p = ""
	local pt = ""
	local n = 0
	
	for i = 1, 3 do
		_, _, _, _, n, pt, _, _ = GetTalentTabInfo(i);
		if (n  > 12) then
			p = pt .. "_" ..  GetShapeshiftForm();
			if UnitInVehicle("player") then
				p = "VEHICLE" 
			end
			if (p ~= faceroll_profile) then
				print ("switching to profile " .. p );
				if (not facerolldb[p]) then
					facerolldb[p] = {}
					print ("warning: empty profile")
				end
			end
		end
	end
	return p;
end
